package com.example.demo2.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.demo2.dao.JsonReader;
import com.example.demo2.util.TransferPage;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

/**
 * @description This class is for user interaction between users and bookingnumber login page
 * @version 2.0
 */

public class PassengerController{
    @FXML
    private TextField BookingNumber;

    @FXML
    private Button SurnameButton;

    @FXML
    private Button LoginButton;

    @FXML
    private Button BackToLogin;

    String path = JsonReader.class.getClassLoader().getResource("json/PassengerData.json").getPath();
    String s = JsonReader.readJsonFile(path);
    JSONObject passengerList = JSON.parseObject(s);
    JSONArray passenger = passengerList.getJSONArray("Passenger");//构建JSONArray数组

    public void surnameController(ActionEvent event) {
        Stage primaryStage = (Stage) SurnameButton.getScene().getWindow();
        primaryStage.hide();
        TransferPage.transfer("surname-id", "Surname Login",415,680);
    }

    public void loginController(ActionEvent event) {

        Stage primaryStage = (Stage) LoginButton.getScene().getWindow();
        primaryStage.hide();
        JSONObject object = null;
        for (int j = 0; j < passenger.size(); j++) {
//            String BookingNum = passenger.getJSONObject(j).getString("BookingNum");
//            System.out.println(BookingNum);

            if (passenger.getJSONObject(j).getString("BookingNum").equals(BookingNumber.getText())) {
                object = passenger.getJSONObject(j);
            }
        }
        if (object!=null){
            InfoController infoController = new InfoController();
            infoController.setText1(object);
        }else{
            TransferPage.transfer("login-error", "Login Error",400,600);
        }

    }

    public void backToLoginController(ActionEvent event) {
        Stage primaryStage = (Stage) BackToLogin.getScene().getWindow();
        primaryStage.hide();
        TransferPage.transfer("passenger", "Passenger Login",400,600);
    }
}
